package com.yeyks.hotel.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yeyks.common.base.Arguments;
import com.yeyks.common.base.PageResult;
import com.yeyks.common.param.hotel.RecommendHotelAddParam;
import com.yeyks.common.param.hotel.RecommendHotelListParam;
import com.yeyks.common.param.hotel.RecommendHotelUpdateParam;
import com.yeyks.common.utils.ObjectConvertUtil;
import com.yeyks.hotel.dal.dao.HotelInfoMapper;
import com.yeyks.hotel.dal.domain.HotelInfo;
import com.yeyks.hotel.dal.domain.HotelRecommend;
import com.yeyks.hotel.dal.dao.HotelRecommendMapper;
import com.yeyks.hotel.dal.domain.HotelRecommendCity;
import com.yeyks.hotel.param.HotelSearchJipAppendPageListParam;
import com.yeyks.hotel.dto.HotelRecommendDTO;
import com.yeyks.hotel.service.HotelRecommendCityService;
import com.yeyks.hotel.service.HotelRecommendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;


/**
 * <p>
 * 推荐酒店表服务实现类
 * </p>
 *
 * @author yun
 * @since 2019-12-24
 */
@Service
public class HotelRecommendServiceImpl extends ServiceImpl<HotelRecommendMapper, HotelRecommend> implements HotelRecommendService {

    @Autowired
    private HotelInfoMapper hotelInfoMapper;
    @Autowired
    private HotelRecommendCityService cityService;

    //酒店展示分页查询
    @Override
    public PageInfo<HotelRecommend> pageList(HotelSearchJipAppendPageListParam param){
        PageHelper.startPage(param.getPageNum(),param.getPageSize(),true,true,true);
        List<HotelRecommend> list = list(
                new QueryWrapper<HotelRecommend>()
                        .eq(Objects.nonNull(param.getCityId()),HotelRecommend.CITY_ID,param.getCityId())
                        .orderByDesc(HotelRecommend.CREATE_TIME)
        );
        PageInfo<HotelRecommend> pageInfo = PageInfo.of(list);
        return pageInfo;
    }

    /**
     * 推荐酒店列表查询
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<HotelRecommendDTO> myList(RecommendHotelListParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<HotelRecommendDTO> hotelRecommends = baseMapper.myList(param);
        return PageResult.getPageResult(hotelRecommends);
    }

    /**
     * 新增
     *
     * @param param
     * @return
     */
    @Override
    public Boolean add(RecommendHotelAddParam param) {
        HotelInfo hotelInfo = hotelInfoMapper.selectById(param.getHotelId());
        Arguments.beanIsNull(hotelInfo, "该酒店不存在");
        HotelRecommend hotelRecommend = BeanUtil.toBean(param, HotelRecommend.class);
        hotelRecommend.setHotelName(hotelInfo.getName());
        hotelRecommend.setHotelAddress(hotelInfo.getAddress());
        hotelRecommend.setHotelId(hotelInfo.getId());
        hotelRecommend.setCityId(param.getCityId());
        Date date = new Date();
        hotelRecommend.setCreateTime(date);
        hotelRecommend.setUpdateTime(date);
        Arguments.isFalse(baseMapper.insert(hotelRecommend) == 1, "新增推荐酒店异常");
        return true;
    }

    /**
     * 编辑酒店
     *
     * @param param
     * @return
     */
    @Override
    public Boolean myUpdate(RecommendHotelUpdateParam param) {
        HotelRecommend hotelRecommend = baseMapper.selectById(param.getId());
        Arguments.beanIsNull(hotelRecommend, "该推荐酒店不存在");
        boolean flag = false;
        if (StrUtil.isNotBlank(param.getImage())) {
            hotelRecommend.setImage(param.getImage());
            flag = true;
        }
        if (StrUtil.isNotBlank(param.getContent())) {
            hotelRecommend.setContent(param.getContent());
            flag = true;
        }
        if (null != param.getCityId()) {
            hotelRecommend.setCityId(param.getCityId());
            flag = true;
        }
        if (null != param.getSalePrice()) {
            hotelRecommend.setSalePrice(param.getSalePrice());
            flag = true;
        }
        if (null != param.getPayPrice()) {
            hotelRecommend.setPayPrice(param.getPayPrice());
            flag = true;
        }
        if (flag) {
            Arguments.isFalse(baseMapper.updateById(hotelRecommend) == 1, "编辑推荐酒店异常");
        }
        return true;
    }
}
